const Api = require('../../api/api.js')
let app = getApp()

Page({
    data: { 
        loginType: '',
        orderType: '',
        ordernumber: '',
        shareuserid: '',
        status: '',
    },
    onLoad: function (options) {
        let self = this;
        console.log(options,'auth界面的options')
        self.setData({
            loginType: options.loginType
        })
         //礼物查收页面进入 
        if (options.loginType != undefined && options.loginType != null && options.loginType != '' && options.loginType == 'forwarding'){
            self.setData({
                orderType: options.orderType,
                ordernumber: options.ordernumber,
                shareuserid: options.shareuserid,
                status: options.status,
            })
        }
    },
    /**
     * userInfo 赋值给globalData和缓存userInfo
     * rtype: "login" 服务器二次验证方式
     * success token 赋值给globalData和缓存userInfo
     */
    userInfoHandler: function (e) {
        let self = this;
        //globalData.code 无数据 就从缓存里面读取
        if (app.globalData.code == undefined || app.globalData.code == null || app.globalData.code == '') {
            app.globalData.code = wx.getStorageSync('code');
        }
        app.globalData.userInfo = e.detail.userInfo

        wx.setStorageSync( 'userInfo', e.detail.userInfo )
        let data = {
            rtype: "login",
            code: app.globalData.code,   
            encryptedData: e.detail.encryptedData,
            iv: e.detail.iv,
            rawData: e.detail.rawData,
            signature: e.detail.signature
        }
        console.log(data,'授权参数')
        Api.get(app.globalData.loginDomain, data)
            .then(res => {
                console.log('------token-----------', res)
                console.log(self.data.orderType, self.data.ordernumber, self.data.shareuserid, self.data.status,)
                app.globalData.token = res.data.data.token;
                wx.setStorageSync('token', res.data.data.token)
                if (self.data.loginType == 'index') {//返回到首页
                    wx.redirectTo({
                        url: '/pages/index/index'
                    })
                } else if (self.data.loginType == 'forwarding') {//返回到礼物查收页面
                    wx.redirectTo({
                        url: '/pages/forwarding/forwarding?orderType=' + self.data.orderType + ' &ordernumber=' + self.data.ordernumber + ' &shareuserid=' + self.data.shareuserid 
                    })
                }
            })
            .catch(err => { console.log(err) })
        }
})